package com.xmu.edu.tisp.contract.plantapplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.xmu.edu.tisp.R;

public class SelectAllCheckboxAdapter extends ArrayAdapter {
	List<HashMap<String, String>> list;
	HashMap<String, String> checked = new HashMap<String, String>();
	String type = null;
	int isAll = 0;
	CheckBox allCheck;
	Context context;
	

	public SelectAllCheckboxAdapter(Context context, List<HashMap<String, String>> list , CheckBox allcheck) {
		super(context, R.layout.select_field_item, list);
		this.list = list;
		this.context = context;
		allCheck = allcheck;
		for(int i=0 ; i<list.size();i++){
			checked.put(i+"",list.get(i).get("FIELD_BLOCK_CODE") );
		}
		isAll=list.size();
		
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View item = inflater.inflate(R.layout.select_field_item, parent,
				false);
		TextView field_code = (TextView) item.findViewById(R.id.SFI_field_code);
		TextView field_manager = (TextView) item.findViewById(R.id.SFI_field_manager);
		CheckBox checkbox = (CheckBox) item.findViewById(R.id.SFI_checkBox);

		HashMap<String, String> map = list.get(position);
		field_code.setText((CharSequence) map.get("FIELD_BLOCK_CODE"));
		field_manager.setText((CharSequence) map.get("FIELD_MANAGER"));
		checkbox.setOnCheckedChangeListener(new CheckedListener(position));
		
		if (checked.containsKey(position + "")) {
			checkbox.setChecked(true);
		} else {
			checkbox.setChecked(false);
		}
		if (map.get("FIELD_BLOCK_CODE").equals("")) {
			checkbox.setVisibility(IGNORE_ITEM_VIEW_TYPE);

		}
		return item;
	}

	public ArrayList<String> getChecked() {
		ArrayList<String> Field_CD = new ArrayList<String>();
		Iterator<String> it = checked.keySet().iterator();
		while (it.hasNext()) {
			String s = it.next();

			Field_CD.add(checked.get(s));
		}
		return Field_CD;
	}
	

	class CheckedListener implements OnCheckedChangeListener {
		int position;

		public CheckedListener(int position) {
			this.position = position;
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			
			if (isChecked == true) {
				
				checked.put(position+"",list.get(position).get("FIELD_BLOCK_CODE") );
				if(checked.size()== isAll){
					allCheck.setClickable(false);
					allCheck.setChecked(true);
					allCheck.setClickable(true);
				}
				else 
				{allCheck.setClickable(false);
			    allCheck.setChecked(false);
			     allCheck.setClickable(true);}
			} else {
				
				    allCheck.setClickable(false);
					allCheck.setChecked(false);
					allCheck.setClickable(true);
				if (checked.containsKey(position + "")) {
					checked.remove(position + "");
					
					}

				}
			}

		}
	

	

}